using System.Collections;
using UnityEngine;
using System;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class UIFrameHelper : MonoBehaviour
{
    /// <summary>
    /// 获取动画播放状态
    /// </summary>
    /// <param name="animator">动画管理器</param>
    /// <param name="animationName">需要判断的动画名称</param>
    /// <param name="playCallBack">动画执行完成回调</param>
    /// <param name="layerIndex">动画所再层级</param>
    /// <returns></returns>
    public void AnimationPlayAndInvoke(Animator animator, string animationName, Action playCallBack, int layerIndex = 0) 
    {
        StartCoroutine(AnimaState(animator, animationName, playCallBack, layerIndex));
    }

    /// <summary>
    /// 延迟执行事件
    /// </summary>
    public void AsyncUIAwait(float time, Action callBack) 
    {
        StartCoroutine(UIAwait(time, callBack));
    }


    /// <summary>
    /// 异步加载场景
    /// </summary>
    /// <param name="scene"></param>
    /// <param name="autoLoding"></param>
    /// <param name="overCallBack"></param>
    public void AsyncLoadScenes(string scene, bool autoLoding = false, Action<AsyncOperation> overCallBack = null) 
    {
        StartCoroutine(AsyncLoad(scene, autoLoding, overCallBack));
    }

    /// <summary>
    /// 设置图片颜色
    /// </summary>
    /// <param name="image"></param>
    /// <returns></returns>
    public void AsyncSetColor(Image image, Color color, Action callBack)
    {
        StopAllCoroutines();
        StartCoroutine(SetColor(image, color, callBack));
    }

    IEnumerator UIAwait(float time,Action callBack)
    {
        yield return new WaitForSecondsRealtime(time);
        callBack?.Invoke();
    }


    /// <summary>
    /// 检查当前动画状态
    /// </summary>
    /// <param name="animator">动画管理器</param>
    /// <param name="animationName">需要判断的动画名称</param>
    /// <param name="playCallBack">动画执行完成回调</param>
    /// <param name="layerIndex">动画所再层级</param>
    /// <returns></returns>
    IEnumerator AnimaState(Animator animator,string animationName,Action playCallBack,int layerIndex=0) 
    {
        animator.Play(animationName);
        yield return new WaitForEndOfFrame();
        AnimatorStateInfo state = animator.GetCurrentAnimatorStateInfo(layerIndex);
        while (state.IsName(animationName) && state.normalizedTime < 1)
        {
            state = animator.GetCurrentAnimatorStateInfo(layerIndex);
            yield return new WaitForEndOfFrame();
        }
        playCallBack?.Invoke();
        yield return null;
    }

    /// <summary>
    /// 异步加载场景
    /// </summary>
    /// <param name="scene">场景名称</param>
    /// <returns></returns>
    IEnumerator AsyncLoad(string scene, bool autoLoding = false,Action<AsyncOperation> overCallBack=null)
    {
        UnityEngine.AsyncOperation operation = SceneManager.LoadSceneAsync(scene);
        operation.allowSceneActivation = autoLoding;
        while (operation.progress < 0.9f)
        {
            yield return new WaitForEndOfFrame();
        }
        overCallBack?.Invoke(operation);
        yield return null;
    }

    /// <summary>
    /// 设置图片颜色
    /// </summary>
    /// <param name="image"></param>
    /// <returns></returns>
    IEnumerator SetColor(Image image,Color color,Action callBack)
    {
        Color taomColor = new Color(color.r, color.g, color.g, color.a);
        if (image.color.a > taomColor.a)
        {
            while (image.color.a > taomColor.a)
            {
                image.color = new Color(taomColor.r, taomColor.g, taomColor.b, image.color.a - 0.05f);
                yield return new WaitForFixedUpdate();
            }
        }
        else if (image.color.a < taomColor.a)
        {
            while (image.color.a < taomColor.a)
            {
                image.color = new Color( taomColor.r, taomColor.g, taomColor.b, image.color.a + 0.05f);
                yield return new WaitForFixedUpdate();
            }
        }

        callBack?.Invoke();
        yield return null;
    }
}
